package org.example;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        try {
            FileOutputStream out = new FileOutputStream("d:\\success.xls");
            HSSFCellStyle headerStyle = wb.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);


            /*String[] textlist = {"考试检测","政策发布","其他事件"};
            sheet = setHSSFValidation(sheet,textlist,1,500,0,0);*/
            /*String[] list1 = {"单项考试","其他政策"};*/
            /*sheet = setCellValue(sheet,0,1,"类型",headerStyle);*/
            /*setHSSFValidation(sheet,list1,1,500,1,1);*/
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static HSSFSheet setHSSFValidation(HSSFSheet sheet,String[] textlist,int firstRow,int endRow,int firstCol,int endCol){
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist);
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow,firstCol,endCol);
        HSSFDataValidation dataValidation = new HSSFDataValidation(regions,constraint);
        sheet.addValidationData(dataValidation);
        HSSFRow row = sheet.createRow(firstRow);
        HSSFCell cell = row.createCell(firstCol);
        HSSFCellStyle cellStyle = cell.getCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return sheet;
    }

    public static HSSFSheet setHSSFPrompt(HSSFSheet sheet,String promptTitle,String promptContent,int firstRow,int endRow,int firstCol,int endCol){
        DVConstraint constraint = DVConstraint.createCustomFormulaConstraint("BB1");
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow,firstCol,endCol);
        HSSFDataValidation dataValidation = new HSSFDataValidation(regions,constraint);
        dataValidation.createPromptBox(promptTitle,promptContent);
        sheet.addValidationData(dataValidation);
        return sheet;
    }


}
